<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Taisan extends MY_Controller {

	public $data 	= 	array();
 	public $_template = array();
	public function __construct() {

		parent::__construct(); //for CI 2.x users 
		$this->load->module_model('member', 'taisan_model', 'taisan');
		$this->load->helper('text');
		$this->user = $this->access->get_user();
		$this->load->helper('url');
		$this->access->restrict();
		$this->load->dbutil();
	}
	
	//Cpnel Quản lý Tài sản
	public function index() 
	{
		$data['ItemID'] = 'User';
		$data['title'] = 'Quản lý Tài sản';
		$data['description'] = 'Quản lý Tài sản';
		$this->_template['page']	= 'users/taisan/cpanel';
		$this->system_library->load($this->_template['page'], $data);
	
	}
	
	public function download_report_ts()
	{
		$filename = 'bao_cao_tong_hop_tai_san_t19';
		$title ='BÁO CÁO TỔNG HỢP TÀI SẢN';
		$listdata	=	$this->taisan->get_report_tai_san(0,0,0);
		if($listdata)
		{
			$return = '';
			$return = $this->generate_report_tong_hop($listdata,$filename,$title);
		}
	}
	
	
	public function generate_report_tong_hop($data,$filename,$title)
	{
	
		$arrT = array();
		$arrT[1] = 'A';
		$arrT[2] = 'B';
		$arrT[3] = 'C';
		$arrT[4] = 'D';
		$arrT[5] = 'E';
		$arrT[6] = 'F';
		$arrT[7] = 'G';
		$arrT[8] = 'H';
		$arrT[9] = 'I';
		$arrT[10] = 'J';
		$arrT[11] = 'K';
		$arrT[12] = 'L';
		$arrT[13] = 'M';
		$arrT[14] = 'N';
		$arrT[15] = 'O';
		$arrT[16] = 'P';
		$arrT[17] = 'Q';
		$arrT[18] = 'R';
		$arrT[19] = 'S';
		$arrT[20] = 'T';
		$arrT[21] = 'U';
		$arrT[22] = 'V';
		$arrT[23] = 'W';
		$arrT[24] = 'X';
		$arrT[25] = 'Y';
		$arrT[26] = 'Z';
		$arrT[27] = 'AA';
		$arrT[28] = 'AB';
		$arrT[29] = 'AC';
		$arrT[30] = 'AD';
		$arrT[31] = 'AE';
		$arrT[32] = 'AF';
		$arrT[33] = 'AG';
		$arrT[34] = 'AH';
		$arrT[35] = 'AI';
		$arrT[36] = 'AK';
		$arrT[37] = 'AL';
	
			
		//load our new PHPExcel library
		$this->load->library('exzel');
		//activate worksheet number 1
		$this->exzel->setActiveSheetIndex(0);
		//name the worksheet
		$this->exzel->getActiveSheet()->setTitle('Bao_Cao');
		//set cell A1 content with some text
		$this->exzel->getActiveSheet()->setCellValue('A1', $title);
	
		$this->exzel->getActiveSheet()->getStyle('A1')->getFont()->setSize(18);
		//make the font become bold
		$this->exzel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
	
		$this->exzel->getActiveSheet()->getColumnDimension('A')->setWidth(5);
		$this->exzel->getActiveSheet()->setCellValue('A4', 'TT');
		$this->exzel->getActiveSheet()->getColumnDimension('B')->setWidth(15);
		$this->exzel->getActiveSheet()->setCellValue('B4', 'MÃ TS');
		$this->exzel->getActiveSheet()->getColumnDimension('C')->setWidth(40);
		$this->exzel->getActiveSheet()->setCellValue('C4', 'Tên TS');
		$this->exzel->getActiveSheet()->getColumnDimension('D')->setWidth(30);
		$this->exzel->getActiveSheet()->setCellValue('D4', 'Nhóm TS');
		$this->exzel->getActiveSheet()->getColumnDimension('E')->setWidth(15);
		$this->exzel->getActiveSheet()->setCellValue('E4', 'Nguyên giá (vnđ)');
		$this->exzel->getActiveSheet()->getColumnDimension('F')->setWidth(15);
		$this->exzel->getActiveSheet()->setCellValue('F4', 'Người Quản lý');
		$this->exzel->getActiveSheet()->getColumnDimension('G')->setWidth(20);
		$this->exzel->getActiveSheet()->setCellValue('G4', 'Thời gian khấu hao');
		$this->exzel->getActiveSheet()->getColumnDimension('H')->setWidth(15);
		$this->exzel->getActiveSheet()->setCellValue('H4', 'Tình trạng');
	
	
	
		// INSERT DIEM
		$stt = 1;
		$irow = 6;
			
		foreach($data as $item)
		{
			$this->exzel->getActiveSheet()->setCellValue('A'.$irow, $stt);
			$this->exzel->getActiveSheet()->setCellValue('B'.$irow, $item['code']);
			$this->exzel->getActiveSheet()->setCellValue('C'.$irow, $item['name']);
			$this->exzel->getActiveSheet()->setCellValue('D'.$irow, $item['hang_muc_name']);
			$this->exzel->getActiveSheet()->setCellValue('E'.$irow, number_format($item['nguyengia']));
			$this->exzel->getActiveSheet()->setCellValue('F'.$irow, $item['tengv']);
			$this->exzel->getActiveSheet()->setCellValue('G'.$irow, $item['thoigiankhauhao']. ' tháng');
			$this->exzel->getActiveSheet()->setCellValue('H'.$irow, get_typets($item['tinhtrang']));
	
			$irow++;
			$stt++;
	
		}
	
	
		$filename=$filename.'.xls'; //save our workbook as this file name
		header('Content-Type: application/vnd.ms-excel'); //mime type
		header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
		header('Cache-Control: max-age=0'); //no cache
			
		//save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
		//if you want to save it as .XLSX Excel 2007 format
		$objWriter = PHPExcel_IOFactory::createWriter($this->exzel, 'Excel5');
		//force user to download the Excel file without writing it to server's HD
		//$objWriter->save("nameoffile.xls");
		$objWriter->save('php://output');
		exit();
	}
	
	public function list_taisan()
	{
		$data['ItemID'] = 'User';
		$data['title'] = 'Danh sách Tài sản, TTB nhà Trường';
		$data['description'] = 'Danh sách Tài sản, TTB nhà Trường';
	
		$this->load->library('basic_library');
		$this->load->library('pagination');
		$this->load->helper('url');
		$config = array();
		$config['base_url'] 	= base_url().'member/taisan/list_taisan/';
		$config['total_rows'] 	= $this->taisan->count_all_tai_san(0);
		$config['per_page'] 	= 100;
		$config['uri_segment']	= 4;
		$config['num_links'] = 5;
		$config['first_link'] = 'Đầu tiên';
		$config['last_link'] = 'Cuối cùng';
		$config['next_link'] = 'Tiếp theo';
		$config['prev_link'] = 'Lùi lại';
		$this->pagination->initialize($config);
		$page = ($this->uri->segment(4))? $this->uri->segment(4):0;
		$data['sotrang']	=	$this->uri->segment(4);
		$data['alldata'] 	= $this->taisan->get_list_taisan($config['per_page'], $page);
		$data['links'] 		= $this->pagination->create_links();
	
		$this->_template['page']	= 'users/taisan/taisan_list';
		$this->system_library->load($this->_template['page'], $data);
	
	}
	
	//Khóa Tài sản
	public function submit_taisan()
	{
		//echo $this->input->post('btnAction');
		//var_dump($this->input->post('check')); exit();
		$ids = $this->input->post('checked');
		//$arr = explode(",", $ids);
		$json = array();
		foreach($ids as $ar)
		{
			if($this->taisan->submit_taisan($ar) == true)
			{
				$json[$ar]['result'] = 1;
				$json[$ar]['key'] = $ar;
			}
			else
			{
				$json[$ar]['result'] = 0;
				$json[$ar]['key'] = $ar;
			}
		}
	
		echo json_encode($json);
	}
	
	//Khóa chứng từ
	public function submit_chung_tu_ts()
	{
		//echo $this->input->post('btnAction');
		//var_dump($this->input->post('check')); exit();
		$ids = $this->input->post('checked');
		//$arr = explode(",", $ids);
		$json = array();
		foreach($ids as $ar)
		{
			if($this->taisan->submit_chung_tu_ts($ar) == true)
			{
				$json[$ar]['result'] = 1;
				$json[$ar]['key'] = $ar;
			}
			else
			{
				$json[$ar]['result'] = 0;
				$json[$ar]['key'] = $ar;
			}
		}
	
		echo json_encode($json);
	}
	
	public function report()
	{
		$data['ItemID'] = 'User';
		$data['title'] = 'Báo cáo tài sản - Quản lý sử dụng';
		$data['description'] = 'Báo cáo tài sản - Quản lý sử dụng';
	
		$this->load->library('basic_library');
		$this->load->library('pagination');
		$this->load->helper('url');
		$config = array();
		$config['base_url'] 	= base_url().'member/taisan/report/';
		$config['total_rows'] 	= $this->taisan->count_all_tai_san(1);
		$config['per_page'] 	= 100;
		$config['uri_segment']	= 4;
		$config['num_links'] = 5;
		$config['first_link'] = 'Đầu tiên';
		$config['last_link'] = 'Cuối cùng';
		$config['next_link'] = 'Tiếp theo';
		$config['prev_link'] = 'Lùi lại';
		$this->pagination->initialize($config);
		$page = ($this->uri->segment(4))? $this->uri->segment(4):0;
		$data['sotrang']	=	$this->uri->segment(4);
		$data['alldata'] 	= $this->taisan->get_report_tai_san($config['per_page'], $page,1);
		$data['links'] 		= $this->pagination->create_links();
	
		$this->_template['page']	= 'users/taisan/report';
		$this->system_library->load($this->_template['page'], $data);
	
	}
	
	//Add teacher to giaovien table and create system account
	public function add_taisan()
	{
		$data['ItemID'] = 'User';
		$data['title'] = 'Thêm Tài sản';
		$data['description'] = 'Thêm Tài sản';
		$this->load->helper('form');
		
		$data['hangmuc']	=	$this->taisan->get_all_hang_muc();
		$data['xuatxu']		=	$this->taisan->get_all_xuat_xu();
		//here is validation form
		$this->form_validation->set_rules('code', 'Mã Tài sản', 'required|trim|callback_check_code');
		$this->form_validation->set_rules('name', 'Tên Tài sản', 'required|trim');
		$this->form_validation->set_rules('nguyengia','Nguyên giá','required|trim');
		
		$this->form_validation->set_error_delimiters('', '<br />');
		$button = $this->input->post('typebutton');
		if($this->form_validation->run() == TRUE)
		{
			$returnid = $this->taisan->add_taisan();
			$this->session->set_flashdata('message', lang('successfully_created'));
			if($button == 1)
			{
				redirect('member/taisan/edit_taisan/'.$returnid, 'refresh');
			}
			elseif ($button == 2){
				redirect('member/taisan/', 'refresh');
			}
			
			else{
				redirect('member/taisan/add_taisan', 'refresh');
			}
		}
		
		$this->_template['page']	= 'users/taisan/taisan_add';
		$this->system_library->load($this->_template['page'], $data);
	}
	
	//Update teacher info
	public function edit_taisan($id)
	{
		$data['ItemID'] = 'User';
		$data['title'] = 'Sửa thông tin Tài sản';
		$data['description'] = 'Sửa thông tin Tài sản';
		$this->load->helper('form');
		
		$data['hangmuc']	=	$this->taisan->get_all_hang_muc();
		$data['xuatxu']		=	$this->taisan->get_all_xuat_xu();
		$data['datainfo']	=	$this->taisan->get_tai_san($id);
		
		//here is validation form
		$this->form_validation->set_rules('code', 'Mã Tài sản', 'required|trim');
		$this->form_validation->set_rules('name', 'Tên Tài sản', 'required|trim');
		$this->form_validation->set_rules('nguyengia','Nguyên giá','required|trim');
		
		if($data['datainfo']->code==$this->input->post('code'))
		{
			$this->form_validation->set_rules('code', 'MATS', 'required|trim');
		}
		else
		{
			$this->form_validation->set_rules('code', 'MATS', 'required|trim|callback_check_code');
		}
		
		$this->form_validation->set_error_delimiters('', '<br />');
		$button = $this->input->post('typebutton');
		if($this->form_validation->run() == TRUE)
		{
			$this->taisan->edit_taisan($id);
			$this->session->set_flashdata('message', lang('successfully_created'));
			if($button == 1)
			{
				redirect('member/taisan/edit_taisan/'.$id, 'refresh');
			}
			else {
				redirect('member/taisan/list_taisan/', 'refresh');
			}
		}
		
		$this->_template['page']	= 'users/taisan/taisan_edit';
		$this->system_library->load($this->_template['page'], $data);
	}
	
	public function list_chung_tu()
	{
		$data['ItemID'] = 'User';
		$data['title'] = 'Danh sách Chứng từ, Phiếu bàn giao Tài sản';
		$data['description'] = 'Danh sách Chứng từ, Phiếu bàn giao Tài sản';
	
		$this->load->library('basic_library');
		$this->load->library('pagination');
		$this->load->helper('url');
		$config = array();
		$config['base_url'] 	= base_url().'member/taisan/list_chung_tu/';
		$config['total_rows'] 	= $this->taisan->count_all_chung_tu();
		$config['per_page'] 	= 100;
		$config['uri_segment']	= 4;
		$config['num_links'] = 5;
		$config['first_link'] = 'Đầu tiên';
		$config['last_link'] = 'Cuối cùng';
		$config['next_link'] = 'Tiếp theo';
		$config['prev_link'] = 'Lùi lại';
		$this->pagination->initialize($config);
		$page = ($this->uri->segment(4))? $this->uri->segment(4):0;
		$data['sotrang']	=	$this->uri->segment(4);
		$data['alldata'] 	= $this->taisan->get_list_chung_tu($config['per_page'], $page);
		$data['links'] 		= $this->pagination->create_links();
	
		$this->_template['page']	= 'users/taisan/chung_tu_list';
		$this->system_library->load($this->_template['page'], $data);
	
	}
	
	public function add_chung_tu()
	{
		$data['ItemID'] = 'User';
		$data['title'] = 'Thêm Phiếu bàn giao';
		$data['description'] = 'Thêm Phiếu bàn giao';
		$this->load->helper('form');
	
		$data['taisan']	=	$this->taisan->get_all_tai_san();
		$data['teacher']	=	$this->taisan->get_all_teacher();
		//here is validation form
		
		$this->form_validation->set_rules('name', 'Tên Phiếu', 'required|trim');
		$this->form_validation->set_rules('listIds', 'Vui lòng chọn tài sản', 'required');
		$this->form_validation->set_error_delimiters('', '<br />');
		$button = $this->input->post('typebutton');
		
		// lấy danh sách các tài sản đã được chọn và tách mảng
		$idlist = $this->input->post('listIds');
		
		$results = explode(",", $idlist);
		
		if($this->form_validation->run() == TRUE)
		{
			$returnid = $this->taisan->add_chung_tu($results);
			$this->session->set_flashdata('message', lang('successfully_created'));
			if($button == 1)
			{
				redirect('member/taisan/edit_chung_tu/'.$returnid, 'refresh');
			}
			elseif($button == 2){
				redirect('member/taisan/list_chung_tu/', 'refresh');
			}
			else {
				redirect('member/taisan/add_chung_tu', 'refresh');
			}
		}
	
		$this->_template['page']	= 'users/taisan/chung_tu_add';
		$this->system_library->load($this->_template['page'], $data);
	}
	
	public function edit_chung_tu($chungtuid)
	{
		$data['ItemID'] = 'User';
		$data['title'] = 'Thêm Phiếu bàn giao';
		$data['description'] = 'Thêm Phiếu bàn giao';
		$this->load->helper('form');
	
		$data['taisan']	=	$this->taisan->get_all_tai_san();
		$data['teacher']	=	$this->taisan->get_all_teacher();
		$data['chung_tu']	=	$this->taisan->get_chung_tu($chungtuid);
		$data['chung_tu_item']	=	$this->taisan->get_chung_tu_item($chungtuid);
		//here is validation form
		$this->form_validation->set_rules('name', 'Tên Phiếu', 'required|trim');
		$this->form_validation->set_rules('listIds', 'Vui lòng chọn tài sản', 'required');
	
		$this->form_validation->set_error_delimiters('', '<br />');
		$button = $this->input->post('typebutton');
	
		// lấy danh sách các tài sản đã được chọn và tách mảng
		$idlist = $this->input->post('listIds');
	
		$results = explode(",", $idlist);
	
		if($this->form_validation->run() == TRUE)
		{
			$this->taisan->edit_chung_tu($chungtuid,$results);
			$this->session->set_flashdata('message', lang('successfully_created'));
			if($button == 1)
			{
				redirect('member/taisan/edit_chung_tu/'.$chungtuid, 'refresh');
			}
			elseif($button == 2){
				redirect('member/taisan/list_chung_tu/', 'refresh');
			}
			else {
				redirect('member/taisan/add_chung_tu', 'refresh');
			}
		}
	
		$this->_template['page']	= 'users/taisan/chung_tu_edit';
		$this->system_library->load($this->_template['page'], $data);
	}	
	
	
	public function check_code()
	{
		if($this->taisan->check_code())
		{
			$this->form_validation->set_message('check_code','MATS đã tồn tại, vui lòng chọn lại');
			return false;
		}
		else
		{
	
			return true;
		}
	}
	
	public function search()
	{
		$data['ItemID'] = 'User';
		$data['title'] = 'Tìm kiếm thông tin';
		$data['description'] = 'Tìm kiếm thông tin';
	
		$data['keyword'] = $this->input->post('keyword', true);
		$data['results'] = $this->taisan->search_data($data['keyword']);
	
		$this->_template['page']	= 'users/taisan/taisan_search';
		$this->system_library->load($this->_template['page'], $data);
	}
		
}

/* End of file welcome.php */
/* Location: ./application/controllers/teacher.php */